﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Google.Translates.Toolbar
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:GoogleTranslateToolbar runat=server></{0}:GoogleTranslateToolbar>")]
    public class GoogleTranslateToolbar : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                var s = (String) ViewState["Text"];
                return (s ?? String.Empty);
            }

            set { ViewState["Text"] = value; }
        }

        private Button btnTrans;

        protected void SetDefaultLangItems()
        {
            var nvcLangKeyInfo = new NameValueCollection
                                     {
                                         {"en", "English"},
                                         {"fr", "Finnish"},
                                         {"es", "Spanish"},
                                         {"vi", "Vietnamese"},
                                         {"zh-CN", "中文(简体)"},
                                         {"zh-TW", "中文(繁體)"}
                                     };
            var ddlSrcLangKey = new DropDownList();
            var ddlTrgLangKey = new DropDownList();

            ddlSrcLangKey.Items.Add(new ListItem("Auto", ""));
            for (int i = 0; i < nvcLangKeyInfo.Count; i++)
            {
                ddlSrcLangKey.Items.Add(new ListItem(nvcLangKeyInfo.Get(i), nvcLangKeyInfo.GetKey(i)));
                ddlTrgLangKey.Items.Add(new ListItem(nvcLangKeyInfo.Get(i), nvcLangKeyInfo.GetKey(i)));
            }
            Controls.Add(ddlSrcLangKey);
            Controls.Add(new Label {Text = " To "});
            Controls.Add(ddlTrgLangKey);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            btnTrans = new Button {Text = Text,CausesValidation=false};
            btnTrans.Attributes.Add("onclick", "javascript:return ('True'=='True')");
            Controls.Add(btnTrans);
            btnTrans.Click+=btnTrans_Click;
            var chShowMore = new CheckBox {Checked = false, Text = "Show More" ,ID="chIsShowMore"};
            Controls.Add(chShowMore);
            SetDefaultLangItems();
        }

        protected void btnTrans_Click(object sender, EventArgs eventArgs)
        {
            if (sender is Button)
            {
                ((Button) sender).Text = "SB";
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //foreach (object c in Controls)
            //{
            //    if (c is WebControl)
            //        ((WebControl) c).RenderControl(writer);
            //}
            base.Render(writer);

            //writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
            //writer.RenderBeginTag(HtmlTextWriterTag.Table);
            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.Write("<strong>" + Text + "</strong>");
            //writer.RenderEndTag();
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.AddAttribute(HtmlTextWriterAttribute.Name, "PaymentMethod");
            //writer.AddAttribute(HtmlTextWriterAttribute.Id, "PaymentMethod");
            //writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            //writer.RenderBeginTag(HtmlTextWriterTag.Select);

            //writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
            //writer.RenderBeginTag(HtmlTextWriterTag.Option);
            //writer.Write("Visa");
            //writer.RenderEndTag();

            //writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
            //writer.RenderBeginTag(HtmlTextWriterTag.Option);
            //writer.Write("MasterCard");
            //writer.RenderEndTag();

            //writer.RenderEndTag();
            //writer.RenderEndTag();
            //writer.RenderEndTag();

            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.Write("<strong>" + Text + "</strong>");
            //writer.RenderEndTag();
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.AddAttribute(HtmlTextWriterAttribute.Name, "CreditCardNo");
            //writer.AddAttribute(HtmlTextWriterAttribute.Id, "CreditCardNo");
            //writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            //writer.RenderBeginTag(HtmlTextWriterTag.Input);
            //writer.RenderEndTag();
            //writer.RenderEndTag();
            //writer.RenderEndTag();

            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.Write("<strong>" + Text + "</strong>");
            //writer.RenderEndTag();
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.AddAttribute(HtmlTextWriterAttribute.Name, "CardholderName");
            //writer.AddAttribute(HtmlTextWriterAttribute.Id, "CardholderName");
            //writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            //writer.RenderBeginTag(HtmlTextWriterTag.Input);
            //writer.RenderEndTag();
            //writer.RenderEndTag();
            //writer.RenderEndTag();

            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.Write("<strong>" + Text + "</strong>");
            //writer.RenderEndTag();
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.AddAttribute(HtmlTextWriterAttribute.Name, "Month");
            //writer.AddAttribute(HtmlTextWriterAttribute.Id, "Month");
            //writer.RenderBeginTag(HtmlTextWriterTag.Select);

            //for (int day = 1; day < 13; day++)
            //{
            //    writer.AddAttribute(HtmlTextWriterAttribute.Value, day.ToString());
            //    writer.RenderBeginTag(HtmlTextWriterTag.Option);

            //    if (day < 10)
            //        writer.Write("0" + day.ToString());
            //    else
            //        writer.Write(day);

            //    writer.RenderEndTag();
            //}

            //writer.RenderEndTag();
            //writer.Write("&nbsp;");

            //writer.AddAttribute(HtmlTextWriterAttribute.Name, "Year");
            //writer.AddAttribute(HtmlTextWriterAttribute.Id, "Year");
            //writer.RenderBeginTag(HtmlTextWriterTag.Select);

            //for (int year = 2005; year < 2015; year++)
            //{
            //    writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());
            //    writer.RenderBeginTag(HtmlTextWriterTag.Option);
            //    writer.Write(year);
            //    writer.RenderEndTag();
            //}

            //writer.RenderEndTag();

            //writer.RenderEndTag();
            //writer.RenderEndTag();

            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
            //writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            //writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
            //writer.RenderBeginTag(HtmlTextWriterTag.Input);
            //writer.RenderEndTag();
            //writer.RenderEndTag();
            //writer.RenderEndTag();
            //writer.RenderEndTag();
        }
    }
}